﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Networking;
using System.Threading;
using Microsoft.Xna.Framework;
using Networking.Packets;

namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(" *** Client ****");
            int clientNum=2;
            int locCount=100;
            ClientThread[] clients = new ClientThread[clientNum];
            for (int i = 0; i < clientNum; i++)
            {
                clients[i] = new ClientThread((ulong)i, "Player " + i, locCount);
            }
            for (int i = 0; i < clientNum; i++)
            {
                clients[i].Start();
            }
            string hostip = Console.ReadLine();
        }

        
    }

    class ClientThread
    {
        private UInt64 PlayerId;
        private string DisplayName;
        private int NumLoc;
        private SimpleGameClient Client = new SimpleGameClient();
        private static Random Rand = new Random();
        Thread MyThread;
        List<ulong> knownPlayers = new List<ulong>(10);
        public ClientThread(UInt64 playerId, string displayName, int numLoc)
        {
            PlayerId = playerId;
            DisplayName = displayName;
            NumLoc = numLoc;
            MyThread = new Thread(run);
        }
        public void Start()
        {
            Client.Start();
            MyThread.Start();
        }

        public void run()
        {
            Thread.Sleep(100);
            Client.SendPlayer(PlayerId, DisplayName);
            int i = 0;
            //Wait random time before starting
            Thread.Sleep(Rand.Next(10000));
            while (i < NumLoc)
            {
                Vector3 pos = new Vector3(Rand.Next(100), Rand.Next(100), 1);
                Client.SendLocation(pos, PlayerId);
                //Console.WriteLine(String.Format("{0} sent location {1}", PlayerId, pos));
                Client.Update();
                List<PlayerInfoPacket> players = Client.PlayerInfoPackets;
                foreach (PlayerInfoPacket p in players)
                {
                    Console.WriteLine(String.Format("{0} has joined the game of {1}!", p.DisplayName, DisplayName));
                    knownPlayers.Add(p.PlayerId);
                }
                List<LocationPacket> loc = Client.LocationPackets;
                foreach (LocationPacket l in loc)
                {
                    //Console.WriteLine(String.Format("Client {0} parsing location from {1}", PlayerId, l.PlayerID));
                    if(knownPlayers.Contains(l.PlayerID)){
                       Console.WriteLine(String.Format("{0} is now at location {1}", l.PlayerID, l.Location));
                    }
                }

                System.Threading.Thread.Sleep(1000);
                i++;
            }


        }
    }
}
